import counter from './counter';
import counter1 from './counter1';
// import {combineReducers} from 'redux';

let reducers = {
  counter,
  counter1
}
let reducer = combineReducers(reducers);
export default reducer;

function combineReducers(reducers){
  let reducerKeys = Object.keys(reducers);
  //作用就是从老状态state和action计算新的状态
  //state是合并后的状态
  return function(state={},action){
    const nextState = {}; //新状态对象，总状态就是合并reducer计算出来的状态之和
    for(let key of reducerKeys){
      let reducer = reducers[key];
      nextState[key] = reducer(state[key],action);
    }
    return nextState;
  }
}